package com.ideaction.controller;

import com.alibaba.fastjson.JSONObject;
import com.ideaction.consts.RuleFuncId;
import com.ideaction.game.bus.BIOrder;
import com.ideaction.game.globalconst.KeyPrefix;
import com.ideaction.game.rule.RuleManager;
import com.ideaction.game.utils.FileUtils;
import com.ideaction.globalconst.Result;
import com.ideaction.service.MailService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RTopic;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 测试用
 *
 * @author ZGame
 */
@RestController
@Slf4j
public class TestController {
    
    @Autowired
    private RedissonClient redissonClient;
    
    @Autowired
    private MailService mailService;
    
    @Autowired
    private MongoTemplate playerMongoTemplate;
    
    @Autowired
    private RestTemplate restTemplate;
    
    @GetMapping("/configHotFix")
    public String testXmlHotFix() {
        RTopic biTopic = redissonClient.getTopic(KeyPrefix.SYSTEM_BI);
        BIOrder bIOrder = new BIOrder();
        bIOrder.setOrderId(1);
        String content = "<?xml version=\"1.0\" encoding=\"GBK\"?>\r\n" + "\r\n" + "<objects>\r\n"
                + "  <Box Sid=\"10004\" Name=\"boxname_2\"></Box>\r\n" + "</objects>";
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("className", "BoxFactory");
        jsonObj.put("xmlName", "sample.box.xml");
        jsonObj.put("xmlContent", content);
        bIOrder.setContent(jsonObj.toJSONString());
        biTopic.publish(JSONObject.toJSONString(bIOrder));
        return "ok";
    }
    
    @GetMapping("/testPath")
    public String testPath() {
        log.info(System.getProperty("user.dir"));
        log.info(this.getClass().getResource("").getPath());
        log.info(Thread.currentThread().getClass().getResource("/").getPath().replace("classes", "lib"));
        return "ok";
    }
    
    @GetMapping("/readFile")
    public String readFile() {
        File f = new File(
                "E:\\game\\ideaction-server\\ideaction-game\\src\\main\\java\\com\\ideaction\\test\\TimerTest.java");
        String content = FileUtils.read(f, "UTF-8");
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("javaFileName", "TimerTest.java");
        jsonObj.put("javaContent", content);
        BIOrder bIOrder = new BIOrder();
        bIOrder.setOrderId(2);
        bIOrder.setContent(jsonObj.toJSONString());
        RTopic biTopic = redissonClient.getTopic(KeyPrefix.SYSTEM_BI);
        biTopic.publish(JSONObject.toJSON(bIOrder));
        return "ok";
    }
    
    @GetMapping("/addSysMail")
    public String addSys() {
        // Mail mail = new Mail();
        // mail.setId(2);
        // mail.setContent("sysMai2");
        // RList<Mail> bucketList = redissonClient.getList(KeyPrefix.SYS_MAIL);
        // bucketList.add(mail);
        return "ok";
    }
    
    @GetMapping("/ruleTest")
    public String RuleTest() {
        
        RuleManager.match(RuleFuncId.DATA_CERTIFY, () -> {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("k1", "v1");
            params.put("obj1", new Object());
            return params;
        });
        
        return "ok";
    }
    
    @PostMapping("/GetHoliday")
    public String getHoliday() throws ParseException {
        // String holidayUrl = "http://api.haoshenqi.top/holiday?date=2021";
        // ResponseEntity<JSONArray> responseEntity =
        // restTemplate.getForEntity(holidayUrl, JSONArray.class);
        // JSONArray body = responseEntity.getBody();
        // for(int i = 0;i<body.size();i++){
        // playerMongoTemplate.insert(body.getJSONObject(i), "holidayDate");
        // }
        // 0 正常上班 1周末 2补板 3 节假日
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        String[] weekDays = {"7", "1", "2", "3", "4", "5", "6"};
        List<JSONObject> list = playerMongoTemplate.findAll(JSONObject.class, "holidayDate");
        for (JSONObject jsonObj : list) {
            String date = jsonObj.getString("date");
            Date tmpDate = format.parse(date);
            cal.setTime(tmpDate);
            int w = cal.get(Calendar.DAY_OF_WEEK) - 1; //
            String wd = weekDays[w];
            if (wd.equals("5")) {
                int status = jsonObj.getInteger("status");
                if (status == 0) {
                    jsonObj.put("status", 1);
                    System.out.println(date);
                    playerMongoTemplate.save(jsonObj, "holidayDate");
                }
            }
        }
        Result r = Result.build();
        return r.response();
    }
    @PostMapping("/test")
    public void postTest(HttpServletRequest request,@RequestBody String body){
        System.out.println(body);
    }
}
